<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    html,body{
      width: 100%;
      height: 100%;
    }
    body{
      background-image: url('images/1.jpg');
      background-size:cover;
    }
    div{
      width: 700px;
      margin: 20px auto;
    }
    img{
      width: 200px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div>
    <img src="images/1.jpg" alt="">
    <img src="images/2.jpg" alt="">
    <img src="images/3.jpg" alt="">
  </div>
  <script>
    var imgs =  document.querySelectorAll('img');
    var body = document.querySelector('body')
    for(var i = 0; i < imgs.length; i++){
      imgs[i].onclick = function(){
        //方法一：直接使用原来的图片地址进行拼接 最方便简单
          body.style.backgroundImage = 'url('+this.src+')'
        //方法二：使用lastIndexOf和slice的方法 拼接地址(笨办法)
        var dizhi = this.src.slice(this.src.lastIndexOf('/')+1)
        body.style.backgroundImage = 'url(images/'+dizhi+')'
      }
    }
  </script>
</body>
</html>
